<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

defined('THINK_PATH') or exit();
vendor('Smarty.Smarty#class');

/**
 * Smarty模板引擎驱动 
 * @category   Extend
 * @package  Extend
 * @subpackage  Driver.Template
 * @author    liu21st <liu21st@gmail.com>
 */
class TemplateSmarty{

    /**
     * 渲染模板输出
     * @access public
     * @param string $templateFile 模板文件名
     * @param array $var 模板变量
     * @return void
     */
    public function fetch($templateFile, $var){
        //$templateFile   =   substr($templateFile,strlen(THEME_PATH));
        //vendor('Smarty.Smarty#class');
        $tpl = new Smarty();
        $tpl->caching = C('TMPL_CACHE_ON');
        $tpl->template_dir = THEME_PATH; //dirname($templateFile);
        $tpl->compile_dir = CACHE_PATH;
        $tpl->cache_dir = TEMP_PATH;
        foreach((array) C('TMPL_ENGINE_CONFIG') as $key => $val){
            $tpl->{$key} = $val;
        }
        $tpl->registerResource('tmpl', new Smarty_Resource_Tmpl());
        /*
          // 设置模板目录
          $tpl->setTemplateDir(array(
          dirname($templateFile),
          THEME_PATH,
          TMPL_PATH,
          TEMP_PATH,
          CACHE_PATH
          ));
         */
        $tpl->assign($var);
        $tpl->display('file:' . $templateFile);
    }

}

/**
 * 使用公共目录
 */
class Smarty_Resource_Tmpl extends Smarty_Resource_Custom{

    /**
     * 从数据库中获取一个模板内容及修改时间。
     *
     * @param string $name 模板名称
     * @param string $source 引用的模板资源
     * @param integer $mtime 引用的模板修改时间戳
     * @return void
     */
    protected function fetch($name, &$source, &$mtime){
        if(is_file(TMPL_PATH . $name)){
            $source = file_get_contents(TMPL_PATH . $name);
            $mtime = filemtime(TMPL_PATH . $name);
        }
    }

}
